<template>
  <div class="view-container">
    <el-form :model="queryParam" label-width="auto">
      <el-row :gutter="10">
        <el-col :span="6">
          <el-form-item label="用户名称" prop="nickname">
            <el-select v-model="queryParam.userId" :show-search="true" class="width-control">
              <el-option v-for="item in userInfo" :value="item.id"
                         :label="item.nickname"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" @click="handleQuery">搜索</el-button>
            <el-button @click="resetParams">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <nebula-table :columns="columns" :data="tableData" use-tool-bar border
                  header-row-class-name="header-row-class" :default-page-size="10"
                  show-overflow-tooltip :total="rowsTotal" @change="changePage">
      <template #head-toolbar>
        <el-button type="primary" @click="handleCreate" size="small">添加用户</el-button>
      </template>
      <template #operation="scope">
        <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
        <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
      </template>
    </nebula-table>
    <el-dialog v-model="isShow" title="添加用户">
      <div class="view-container">
        <el-form :model="createParam" :pane="true">
          <el-row :gutter=20>
            <el-col :span="12">
              <el-form-item label="用户" prop="leader">
                <el-select v-model="createParam.userId" :show-search="true" class="width-control">
                  <el-option v-for="item in userInfo" :value="item.id"
                             :label="item.nickname"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="排序" prop="orderNo">
                <el-input-number v-model="createParam.orderNo" :min="0"
                                 class="width-control"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="岗位" prop="deptPost">
                <el-input v-model="createParam.deptPost"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <template #footer>
        <el-button @click="createCancel">取消</el-button>
        <el-button type="primary" @click="createSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import {useRoute} from "vue-router";
import {getNormalUser} from "@/api/sysUser.js";
import {onMounted} from "vue";
import {addOrEditUserToDept, getDeptName, getDeptUser, removeUserFormDept} from "@/api/sysDept.js";
import NebulaTable from "@/components/NebulaTable.vue";
import {ElMessageBox, ElNotification} from "element-plus";

defineOptions({
  name: 'deptDetail'
})


const route = useRoute()
const deptId = route.params.deptId
const deptName = ref()
const queryParam = ref({
  userId: null,
  deptId: deptId,
  pageNum: 1,
  pageSize: 10
})
const isShow = ref(false)
const userInfo = ref([])

const rowsTotal = ref(0)
const tableData = ref([])

const columns = [
  {title: "岗位", key: "deptPost", align: 'center'},
  {title: "姓名", key: "nickname", align: 'center'},
  {title: "电话", key: "userTelephone", align: 'center'},
  {title: "邮箱", key: "userEmail", align: 'center'},
  {title: "添加时间", key: "createTime", align: 'center'},
  {title: "排序", key: "orderNo", align: 'center'},
  {title: "操作", align: 'center', customSlot: 'operation'}
]
const createParam = ref({
  userId: null,
  deptId: deptId,
  orderNo: 1,
  deptPost: null,
})

const createSubmit = () => {
  addOrEditUserToDept(createParam.value).then(res => {
    if (res.code === 200) {
      isShow.value = false
      handleQuery()
      ElNotification({
        title: "操作成功",
        type: "success"
      })
    } else {
      ElNotification({
        title: "创建失败",
        type: "error",
        message: res.msg
      })
    }
  })
}

const createCancel = () => {
  isShow.value = false
  for (let key in createParam.value) {
    switch (key) {
      case "orderNo":
        createParam.value[key] = 1;
        continue;
      case "deptId":
        createParam.value[key] = deptId;
        continue;
      default:
        createParam.value[key] = null;
    }
  }
}


const changePage = (currentPage,pageSize) => {
  queryParam.value.pageNum = currentPage
  queryParam.value.pageSize = pageSize
  handleQuery()
}

const loadDeptName = (id) => {
  getDeptName(id).then(res => {
    deptName.value = res.data
  })
}

const getUserInfo = () => {
  getNormalUser().then(res => {
    if (res.code === 200) userInfo.value = res.data
  })
}

const handleQuery = () => {
  getDeptUser(queryParam.value).then(res => {
    tableData.value = res.data.rows
    rowsTotal.value = res.data.total
  })
}

const resetParams = () => {
  for (let key in queryParam.value) {
    if (key !== "pageNum" && key !== "pageSize" && key !== "deptId") {
      queryParam.value[key] = null;
    }
  }
}

const handleCreate = () => {
  isShow.value = true
}

const handleEdit = (row) => {
  createParam.value = {...row}
  isShow.value = true
}

const handleDelete = (row) => {
  const confirmText = `确定要移除部门【${deptName.value}】下的用户【${row.nickname}】吗?`
  ElMessageBox.confirm(
      confirmText,
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    const removeParam = {
      id: row.id,
    }
    removeUserFormDept(removeParam).then(res => {
      if (res.code === 200) {
        handleQuery()
        ElNotification({
          title: "操作成功",
          type: "success"
        })
      } else {
        ElNotification({
          title: "操作失败",
          type: "error",
          message: res.msg
        })
      }
    })
  })
}


onMounted(() => {
  getUserInfo()
  handleQuery()
  loadDeptName(route.params.deptId)
})
</script>
